home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / kcompletion.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-10  |  35.8 KB  |  1,011 lines

  1. /* This file is part of the KDE libraries
  2.    Copyright (C) 1999,2000 Carsten Pfeiffer <pfeiffer@kde.org>
  3.  
  4.    This library is free software; you can redistribute it and/or
  5.    modify it under the terms of the GNU Library General Public
  6.    License as published by the Free Software Foundation; either
  7.    version 2 of the License, or (at your option) any later version.
  8.  
  9.    This library is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.    Library General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU Library General Public License
  15.    along with this library; see the file COPYING.LIB.  If not, write to
  16.    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17.    Boston, MA 02110-1301, USA.
  18. */
  19.  
  20. #ifndef KCOMPLETION_H
  21. #define KCOMPLETION_H
  22.  
  23. #include <qmap.h>
  24. #include <qptrlist.h>
  25. #include <qobject.h>
  26. #include <qstring.h>
  27. #include <qstringlist.h>
  28. #include <qguardedptr.h>
  29.  
  30. #include "kdelibs_export.h"
  31. #include <kglobalsettings.h>
  32. #include <ksortablevaluelist.h>
  33. #include <kshortcut.h>
  34.  
  35. class KCompTreeNode;
  36. class KCompletionPrivate;
  37. class KCompletionBasePrivate;
  38. class KCompletionMatchesWrapper;
  39. class KCompletionMatches;
  40. class QPopupMenu;
  41.  
  42. /**
  43.  * @short A generic class for completing QStrings
  44.  *
  45.  * This class offers easy use of "auto-completion", "manual-completion" or
  46.  * "shell completion" on QString objects. A common use is completing filenames
  47.  * or URLs (see KURLCompletion()).
  48.  * But it is not limited to URL-completion -- everything should be completable!
  49.  * The user should be able to complete email-addresses, telephone-numbers,
  50.  * commands, SQL queries, ...
  51.  * Every time your program knows what the user can type into an edit-field, you
  52.  * should offer completion. With KCompletion, this is very easy, and if you are
  53.  * using a line edit widget ( KLineEdit), it is even more easy.
  54.  * Basically, you tell a KCompletion object what strings should be completable
  55.  * and whenever completion should be invoked, you call makeCompletion().
  56.  * KLineEdit and (an editable) KComboBox even do this automatically for you.
  57.  *
  58.  * KCompletion offers the completed string via the signal match() and
  59.  * all matching strings (when the result is ambiguous) via the method
  60.  * allMatches().
  61.  *
  62.  * Notice: auto-completion, shell completion and manual completion work
  63.  *         slightly differently:
  64.  *
  65.  * @li auto-completion always returns a complete item as match.
  66.  *     When more than one matching items are available, it will deliver just
  67.  *     the first (depending on sorting order) item. Iterating over all matches
  68.  *     is possible via nextMatch() and previousMatch().
  69.  *
  70.  * @li popup-completion works in the same way, the only difference being that
  71.  *     the completed items are not put into the edit-widget, but into a
  72.  *     separate popup-box.
  73.  *
  74.  * @li manual completion works the same way as auto-completion, the
  75.  *     subtle difference is, that it isn't invoked automatically while the user
  76.  *     is typing, but only when the user presses a special key. The difference
  77.  *     of manual and auto-completion is therefore only visible in UI classes,
  78.  *     KCompletion needs to know whether to deliver partial matches
  79.  *     (shell completion) or whole matches (auto/manual completion), therefore
  80.  * KGlobalSettings::CompletionMan and
  81.  * KGlobalSettings::CompletionAuto have the exact same effect in
  82.  *     KCompletion.
  83.  *
  84.  * @li shell completion works like how shells complete filenames:
  85.  *     when multiple matches are available, the longest possible string of all
  86.  *     matches is returned (i.e. only a partial item).
  87.  *     Iterating over all matching items (complete, not partial) is possible
  88.  *     via nextMatch() and previousMatch().
  89.  *
  90.  * You don't have to worry much about that though, KCompletion handles
  91.  * that for you, according to the setting setCompletionMode().
  92.  * The default setting is globally configured by the user and read
  93.  * from KGlobalSettings::completionMode().
  94.  *
  95.  * A short example:
  96.  * \code
  97.  * KCompletion completion;
  98.  * completion.setOrder( KCompletion::Sorted );
  99.  * completion.addItem( "pfeiffer@kde.org" );
  100.  * completion.addItem( "coolo@kde.org" );
  101.  * completion.addItem( "carpdjih@sp.zrz.tu-berlin.de" );
  102.  * completion.addItem( "carp@cs.tu-berlin.de" );
  103.  *
  104.  * cout << completion.makeCompletion( "ca" ).latin1() << endl;
  105.  * \endcode
  106.  *
  107.  * In shell-completion-mode, this will be "carp"; in auto-completion-
  108.  * mode it will be "carp\@cs.tu-berlin.de", as that is alphabetically
  109.  * smaller.
  110.  * If setOrder was set to Insertion, "carpdjih\@sp.zrz.tu-berlin.de"
  111.  * would be completed in auto-completion-mode, as that was inserted before
  112.  * "carp\@cs.tu-berlin.de".
  113.  *
  114.  * You can dynamically update the completable items by removing and adding them
  115.  * whenever you want.
  116.  * For advanced usage, you could even use multiple KCompletion objects. E.g.
  117.  * imagine an editor like kwrite with multiple open files. You could store
  118.  * items of each file in a different KCompletion object, so that you know (and
  119.  * tell the user) where a completion comes from.
  120.  *
  121.  * Note: KCompletion does not work with strings that contain 0x0 characters
  122.  *       (unicode nul), as this is used internally as a delimiter.
  123.  *
  124.  * You may inherit from KCompletion and override makeCompletion() in
  125.  * special cases (like reading directories/urls and then supplying the
  126.  * contents to KCompletion, as KURLCompletion does), but generally, this is
  127.  * not necessary.
  128.  *
  129.  *
  130.  * @author Carsten Pfeiffer <pfeiffer@kde.org>
  131.  */
  132. class KDECORE_EXPORT KCompletion : public QObject
  133. {
  134.     Q_ENUMS( CompOrder )
  135.     Q_PROPERTY( CompOrder order READ order WRITE setOrder )
  136.     Q_PROPERTY( bool ignoreCase READ ignoreCase WRITE setIgnoreCase )
  137.     Q_PROPERTY( QStringList items READ items WRITE setItems )
  138.     Q_OBJECT
  139.  
  140. public:
  141.     /**
  142.      * Constants that represent the order in which KCompletion performs
  143.      * completion-lookups.
  144.      */
  145.     enum CompOrder { Sorted,    ///< Use alphabetically sorted order
  146.                      Insertion, ///< Use order of insertion
  147.                      Weighted   ///< Use weighted order
  148.     };
  149.  
  150.     /**
  151.      * Constructor, nothing special here :)
  152.      */
  153.     KCompletion();
  154.  
  155.     // FIXME: copy constructor, assignment operator...
  156.  
  157.     /**
  158.      * Destructor, nothing special here, either.
  159.      */
  160.     virtual ~KCompletion();
  161.  
  162.     /**
  163.      * Attempts to find an item in the list of available completions,
  164.      * that begins with @p string. Will either return the first matching item
  165.      * (if there is more than one match) or QString::null, if no match was
  166.      * found.
  167.      *
  168.      * In the latter case, a sound will be issued, depending on
  169.      * isSoundsEnabled().
  170.      * If a match was found, it will also be emitted via the signal
  171.      * match().
  172.      *
  173.      * If this is called twice or more often with the same string while no
  174.      * items were added or removed in the meantime, all available completions
  175.      * will be emitted via the signal #matches().
  176.      * This happens only in shell-completion-mode.
  177.      *
  178.      * @param string the string to complete
  179.      * @return the matching item, or QString::null if there is no matching
  180.      * item.
  181.      * @see slotMakeCompletion
  182.      * @see substringCompletion
  183.      */
  184.     virtual QString makeCompletion( const QString& string );
  185.  
  186.     /**
  187.      * Returns a list of all completion items that contain the given @p string.
  188.      * @param string the string to complete
  189.      * @return a list of items which all contain @p text as a substring,
  190.      * i.e. not necessarily at the beginning.
  191.      *
  192.      * @see makeCompletion
  193.      */
  194.     QStringList substringCompletion( const QString& string ) const;
  195.  
  196.     /**
  197.      * Returns the next item from the matching-items-list.
  198.      * When reaching the beginning, the list is rotated so it will return the
  199.      * last match and a sound is issued (depending on isSoundsEnabled()).
  200.      * @return the next item from the matching-items-list.
  201.      * When there is no match, QString::null is returned and
  202.      * a sound is be issued.
  203.      * @see slotPreviousMatch
  204.      */
  205.     QString previousMatch();
  206.  
  207.     /**
  208.      * Returns the next item from the matching-items-list.
  209.      * When reaching the last item, the list is rotated, so it will return
  210.      * the first match and a sound is issued (depending on
  211.      * isSoundsEnabled()).
  212.      * @return the next item from the matching-items-list.  When there is no
  213.      * match, QString::null is returned and a sound is issued
  214.      * @see slotNextMatch
  215.      */
  216.     QString nextMatch();
  217.  
  218.     /**
  219.      * Returns the last match. Might be useful if you need to check whether
  220.      * a completion is different from the last one.
  221.      * @return the last match. QString::null is returned when there is no
  222.      *         last match.
  223.      */
  224.     virtual const QString& lastMatch() const { return myLastMatch; }
  225.  
  226.     /**
  227.      * Returns a list of all items inserted into KCompletion. This is useful
  228.      * if you need to save the state of a KCompletion object and restore it
  229.      * later.
  230.      *
  231.      * Important note: when order() == Weighted, then every item in the
  232.      * stringlist has its weight appended, delimited by a colon. E.g. an item
  233.      * "www.kde.org" might look like "www.kde.org:4", where 4 is the weight.
  234.      *
  235.      * This is necessary so that you can save the items along with its
  236.      * weighting on disk and load them back with setItems(), restoring its
  237.      * weight as well. If you really don't want the appended weightings, call
  238.      * setOrder( KCompletion::Insertion )
  239.      * before calling items().
  240.      *
  241.      * @return a list of all items
  242.      * @see setItems
  243.      */
  244.     QStringList items() const;
  245.     
  246.     /**
  247.      * Returns true when the completion object contains no entries.
  248.      */
  249.     bool isEmpty() const;
  250.  
  251.     /**
  252.      * Sets the completion mode to Auto/Manual, Shell or None.
  253.      * If you don't set the mode explicitly, the global default value
  254.      * KGlobalSettings::completionMode() is used.
  255.      * KGlobalSettings::CompletionNone disables completion.
  256.      * @param mode the completion mode
  257.      * @see completionMode
  258.      * @see KGlobalSettings::completionMode
  259.      */
  260.     virtual void setCompletionMode( KGlobalSettings::Completion mode );
  261.  
  262.     /**
  263.      * Return the current completion mode.
  264.      * May be different from KGlobalSettings::completionMode(), if you
  265.      * explicitly called setCompletionMode().
  266.      * @return the current completion mode
  267.      * @see setCompletionMode
  268.      */
  269.     KGlobalSettings::Completion completionMode() const {
  270.         return myCompletionMode;
  271.     }
  272.  
  273.     /**
  274.      * KCompletion offers three different ways in which it offers its items:
  275.      * @li in the order of insertion
  276.      * @li sorted alphabetically
  277.      * @li weighted
  278.      *
  279.      * Choosing weighted makes KCompletion perform an implicit weighting based
  280.      * on how often an item is inserted. Imagine a web browser with a location
  281.      * bar, where the user enters URLs. The more often a URL is entered, the
  282.      * higher priority it gets.
  283.      *
  284.      * Note: Setting the order to sorted only affects new inserted items,
  285.      * already existing items will stay in the current order. So you probably
  286.      * want to call setOrder( Sorted ) before inserting items, when you want
  287.      * everything sorted.
  288.      *
  289.      * Default is insertion order.
  290.      * @param order the new order
  291.      * @see order
  292.      */
  293.     virtual void setOrder( CompOrder order );
  294.  
  295.     /**
  296.      * Returns the completion order.
  297.      * @return the current completion order.
  298.      * @see setOrder
  299.      */
  300.     CompOrder order() const { return myOrder; }
  301.  
  302.     /**
  303.      * Setting this to true makes KCompletion behave case insensitively.
  304.      * E.g. makeCompletion( "CA" ); might return "carp\@cs.tu-berlin.de".
  305.      * Default is false (case sensitive).
  306.      * @param ignoreCase true to ignore the case
  307.      * @see ignoreCase
  308.      */
  309.     virtual void setIgnoreCase( bool ignoreCase );
  310.  
  311.     /**
  312.      * Return whether KCompletion acts case insensitively or not.
  313.      * Default is false (case sensitive).
  314.      * @return true if the case will be ignored
  315.      * @see setIgnoreCase
  316.      */
  317.     bool ignoreCase() const { return myIgnoreCase; }
  318.  
  319.     /**
  320.      * Returns a list of all items matching the last completed string.
  321.      * Might take some time, when you have LOTS of items.
  322.      * @return a list of all matches for the last completed string.
  323.      * @see substringCompletion
  324.      */
  325.     QStringList allMatches();
  326.  
  327.     /**
  328.      * Returns a list of all items matching @p string.
  329.      * @param string the string to match
  330.      * @return the list of all matches
  331.      */
  332.     QStringList allMatches( const QString& string );
  333.  
  334.     /**
  335.      * Returns a list of all items matching the last completed string.
  336.      * Might take some time, when you have LOTS of items.
  337.      * The matches are returned as KCompletionMatches, which also
  338.      * keeps the weight of the matches, allowing
  339.      * you to modify some matches or merge them with matches
  340.      * from another call to allWeightedMatches(), and sort the matches
  341.      * after that in order to have the matches ordered correctly.
  342.      *
  343.      * @return a list of all completion matches
  344.      * @see substringCompletion
  345.      */
  346.     KCompletionMatches allWeightedMatches();
  347.  
  348.     /**
  349.      * Returns a list of all items matching @p string.
  350.      * @param string the string to match
  351.      * @return a list of all matches
  352.      */
  353.     KCompletionMatches allWeightedMatches( const QString& string );
  354.  
  355.     /**
  356.      * Enables/disables playing a sound when
  357.      * @li makeCompletion() can't find a match
  358.      * @li there is a partial completion (= multiple matches in
  359.      *     Shell-completion mode)
  360.      * @li nextMatch() or previousMatch() hit the last possible
  361.      *     match -> rotation
  362.      *
  363.      * For playing the sounds, KNotifyClient() is used.
  364.      *
  365.      * @param enable true to enable sounds
  366.      * @see isSoundsEnabled
  367.      */
  368.     virtual void setEnableSounds( bool enable ) { myBeep = enable; }
  369.  
  370.     /**
  371.      * Tells you whether KCompletion will play sounds on certain occasions.
  372.      * Default is enabled.
  373.      * @return true if sounds are enabled
  374.      * @see enableSounds
  375.      * @see disableSounds
  376.      */
  377.     bool isSoundsEnabled() const { return myBeep; }
  378.  
  379.     /**
  380.      * Returns true when more than one match is found.
  381.      * @return true if there are more than one match
  382.      * @see multipleMatches
  383.      */
  384.     bool hasMultipleMatches() const { return myHasMultipleMatches; }
  385.  
  386. #ifndef KDE_NO_COMPAT
  387.     /**
  388.      * @deprecated
  389.      * @see setEnableSounds
  390.      */
  391.     void enableSounds() { myBeep = true; }
  392.  
  393.     /**
  394.      * @deprecated
  395.      * @see setEnableSounds
  396.      */
  397.     void disableSounds() { myBeep = false; }
  398. #endif
  399.     
  400. public slots:
  401.     /**
  402.      * Attempts to complete "string" and emits the completion via match().
  403.      * Same as makeCompletion() (just as a slot).
  404.      * @param string the string to complete
  405.      * @see makeCompletion
  406.      */
  407.     void slotMakeCompletion( const QString& string ) {
  408.         (void) makeCompletion( string );
  409.     }
  410.  
  411.     /**
  412.      * Searches the previous matching item and emits it via match().
  413.      * Same as previousMatch() (just as a slot).
  414.      * @see previousMatch
  415.      */
  416.     void slotPreviousMatch() {
  417.         (void) previousMatch();
  418.     }
  419.  
  420.     /**
  421.      * Searches the next matching item and emits it via match().
  422.      * Same as nextMatch() (just as a slot).
  423.      * @see nextMatch
  424.      */
  425.     void slotNextMatch() {
  426.         (void) nextMatch();
  427.     }
  428.  
  429.     // FIXME ###: KDE4: unify the nomenclature.  We have insertItems, addItem,
  430.     //            setItems...
  431.     /**
  432.      * Inserts @p items into the list of possible completions.
  433.      * Does the same as setItems(), but does not call clear() before.
  434.      * @param items the items to insert
  435.      */
  436.     void insertItems( const QStringList& items );
  437.  
  438.     /**
  439.      * Sets the list of items available for completion. Removes all previous
  440.      * items.
  441.      *
  442.      * Notice: when order() == Weighted, then the weighting is looked up for
  443.      * every item in the stringlist. Every item should have ":number" appended,
  444.      * where number is an unsigned integer, specifying the weighting.
  445.      *
  446.      * If you don't like this, call
  447.      * setOrder( KCompletion::Insertion )
  448.      * before calling setItems().
  449.      *
  450.      * @param list the list of items that are available for completion
  451.      * @see items
  452.      */
  453.     virtual void setItems( const QStringList& list);
  454.  
  455.     /**
  456.      * Adds an item to the list of available completions.
  457.      * Resets the current item-state ( previousMatch() and nextMatch()
  458.      * won't work anymore).
  459.      * @param item the item to add
  460.      */
  461.     void addItem( const QString& item);
  462.  
  463.     /**
  464.      * Adds an item to the list of available completions.
  465.      * Resets the current item-state ( previousMatch() and nextMatch()
  466.      * won't work anymore).
  467.      *
  468.      * Sets the weighting of the item to @p weight or adds it to the current
  469.      * weighting if the item is already available. The weight has to be greater
  470.      * than 1 to take effect (default weight is 1).
  471.      * @param item the item to add
  472.      * @param weight the weight of the item, default is 1
  473.      */
  474.     void addItem( const QString& item, uint weight );
  475.  
  476.     /**
  477.      * Removes an item from the list of available completions.
  478.      * Resets the current item-state ( previousMatch() and nextMatch()
  479.      * won't work anymore).
  480.      * @param item the item to remove
  481.      */
  482.     void removeItem( const QString& item);
  483.  
  484.     /**
  485.      * Removes all inserted items.
  486.      */
  487.     virtual void clear();
  488.  
  489.  
  490. signals:
  491.     /**
  492.      * The matching item. Will be emitted by makeCompletion(),
  493.      * previousMatch() or nextMatch(). May be QString::null if there
  494.      * is no matching item.
  495.      * @param item the match, or QString::null if there is none
  496.      */
  497.     void match( const QString& item);
  498.  
  499.     /**
  500.      * All matching items. Will be emitted by makeCompletion() in shell-
  501.      * completion-mode, when the same string is passed to makeCompletion twice
  502.      * or more often.
  503.      * @param matchlist the list of matches
  504.      */
  505.     void matches( const QStringList& matchlist);
  506.  
  507.     /**
  508.      * This signal is emitted, when calling makeCompletion() and more than
  509.      * one matching item is found.
  510.      * @see hasMultipleMatches
  511.      */
  512.     void multipleMatches();
  513.  
  514. protected:
  515.     /**
  516.      * This method is called after a completion is found and before the
  517.      * matching string is emitted. You can override this method to modify the
  518.      * string that will be emitted.
  519.      * This is necessary e.g. in KURLCompletion(), where files with spaces
  520.      * in their names are shown escaped ("filename\ with\ spaces"), but stored
  521.      * unescaped inside KCompletion.
  522.      * Never delete that pointer!
  523.      *
  524.      * Default implementation does nothing.
  525.      * @param match the match to process
  526.      * @see postProcessMatches
  527.      */
  528.     virtual void postProcessMatch( QString *match ) const { Q_UNUSED(match) }
  529.  
  530.     /**
  531.      * This method is called before a list of all available completions is
  532.      * emitted via #matches. You can override this method to modify the
  533.      * found items before match() or #matches are emitted.
  534.      * Never delete that pointer!
  535.      *
  536.      * Default implementation does nothing.
  537.      * @param matches the matches to process
  538.      * @see postProcessMatch
  539.      */
  540.     virtual void postProcessMatches( QStringList * matches ) const { Q_UNUSED(matches)}
  541.  
  542.     /**
  543.      * This method is called before a list of all available completions is
  544.      * emitted via #matches. You can override this method to modify the
  545.      * found items before #match() or #matches() are emitted.
  546.      * Never delete that pointer!
  547.      *
  548.      * Default implementation does nothing.
  549.      * @param matches the matches to process
  550.      * @see postProcessMatch
  551.      */
  552.     virtual void postProcessMatches( KCompletionMatches * matches ) const {Q_UNUSED(matches)}
  553.  
  554. private:
  555.     void            addWeightedItem( const QString& );
  556.     QString         findCompletion( const QString& string );
  557.     void            findAllCompletions( const QString&,
  558.                                         KCompletionMatchesWrapper *matches,
  559.                                         bool& hasMultipleMatches ) const;
  560.  
  561.     void extractStringsFromNode( const KCompTreeNode *,
  562.                                  const QString& beginning,
  563.                                  KCompletionMatchesWrapper *matches,
  564.                                  bool addWeight = false ) const;
  565.     void extractStringsFromNodeCI( const KCompTreeNode *,
  566.                                    const QString& beginning,
  567.                                    const QString& restString,
  568.                                    KCompletionMatchesWrapper *matches) const;
  569.  
  570.     enum        BeepMode { NoMatch, PartialMatch, Rotation };
  571.     void        doBeep( BeepMode ) const;
  572.  
  573.     KGlobalSettings::Completion myCompletionMode;
  574.  
  575.     CompOrder       myOrder;
  576.     QString         myLastString;
  577.     QString         myLastMatch;
  578.     QString         myCurrentMatch;
  579.     KCompTreeNode * myTreeRoot;
  580.     QStringList     myRotations;
  581.     bool            myBeep;
  582.     bool            myIgnoreCase;
  583.     bool            myHasMultipleMatches;
  584.     uint            myRotationIndex;
  585.  
  586.  
  587. protected:
  588.     virtual void virtual_hook( int id, void* data );
  589. private:
  590.     KCompletionPrivate *d;
  591. };
  592.  
  593. // some more helper stuff
  594. typedef KSortableValueList<QString> KCompletionMatchesList;
  595. class KCompletionMatchesPrivate;
  596.  
  597. /**
  598.  * This structure is returned by KCompletion::allWeightedMatches .
  599.  * It also keeps the weight of the matches, allowing
  600.  * you to modify some matches or merge them with matches
  601.  * from another call to allWeightedMatches(), and sort the matches
  602.  * after that in order to have the matches ordered correctly
  603.  *
  604.  * Example (a simplified example of what Konqueror's completion does):
  605.  * \code
  606.  * KCompletionMatches matches = completion->allWeightedMatches( location );
  607.  * if( !location.startsWith( "www." ))
  608.  matches += completion->allWeightedmatches( "www." + location" );
  609.  * matches.removeDuplicates();
  610.  * QStringList list = matches.list();
  611.  * \endcode
  612.  *
  613.  * @short List for keeping matches returned from KCompletion
  614.  */
  615. class KDECORE_EXPORT KCompletionMatches : public KCompletionMatchesList
  616. {
  617. public:
  618.     KCompletionMatches( bool sort );
  619.     /**
  620.      * @internal
  621.      */
  622.     KCompletionMatches( const KCompletionMatchesWrapper& matches );
  623.     ~KCompletionMatches();
  624.     /**
  625.      * Removes duplicate matches. Needed only when you merged several matches
  626.      * results and there's a possibility of duplicates.
  627.      */
  628.     void removeDuplicates();
  629.     /**
  630.      * Returns the matches as a QStringList.
  631.      * @param sort if false, the matches won't be sorted before the conversion,
  632.      *             use only if you're sure the sorting is not needed
  633.      * @return the list of matches
  634.      */
  635.     QStringList list( bool sort = true ) const;
  636.     /**
  637.      * If sorting() returns false, the matches aren't sorted by their weight,
  638.      * even if true is passed to list().
  639.      * @return true if the matches won't be sorted
  640.      */
  641.     bool sorting() const {
  642.         return _sorting;
  643.     }
  644. private:
  645.     bool _sorting;
  646.     KCompletionMatchesPrivate* d;
  647. };
  648.  
  649. /**
  650.  * An abstract base class for adding a completion feature
  651.  * into widgets.
  652.  *
  653.  * This is a convenience class that provides the basic functions
  654.  * needed to add text completion support into widgets.  All that
  655.  * is required is an implementation for the pure virtual function
  656.  * setCompletedText.  Refer to KLineEdit or KComboBox
  657.  * to see how easily such support can be added using this as a base
  658.  * class.
  659.  *
  660.  * @short An abstract class for adding text completion support to widgets.
  661.  * @author Dawit Alemayehu <adawit@kde.org>
  662.  */
  663. class KDECORE_EXPORT KCompletionBase
  664. {
  665. public:
  666.     /**
  667.      * Constants that represent the items whose short-cut
  668.      * key-binding is programmable.  The default key-bindings
  669.      * for these items are defined in KStdAccel.
  670.      */
  671.     enum KeyBindingType {
  672.         /**
  673.          * Text completion (by default Ctrl-E).
  674.          */
  675.         TextCompletion,
  676.         /**
  677.          * Switch to previous completion (by default Ctrl-Up).
  678.          */
  679.         PrevCompletionMatch,
  680.         /**
  681.          * Switch to next completion (by default Ctrl-Down).
  682.          */
  683.         NextCompletionMatch,
  684.         /**
  685.          * Substring completion (by default Ctrl-T).
  686.          */
  687.         SubstringCompletion
  688.     };
  689.  
  690.  
  691.     // Map for the key binding types mentioned above.
  692.     typedef QMap<KeyBindingType, KShortcut> KeyBindingMap;
  693.  
  694.     /**
  695.      * Default constructor.
  696.      */
  697.     KCompletionBase();
  698.  
  699.     /**
  700.      * Destructor.
  701.      */
  702.     virtual ~KCompletionBase();
  703.     
  704.     /**
  705.      * Returns a pointer to the current completion object.
  706.      *
  707.      * If the completion object does not exist, it is automatically created and
  708.      * by default handles all the completion signals internally unless @p hsig
  709.      * is set to false. It is also automatically destroyed when the destructor
  710.      * is called. You can change this default behavior using the 
  711.      * @ref setAutoDeleteCompletionObject and @ref setHandleSignals member 
  712.      * functions.
  713.      *
  714.      * See also @ref compObj.
  715.      *
  716.      * @param hsig if true, handles completion signals internally.
  717.      * @return a pointer the completion object.
  718.      */
  719.     KCompletion* completionObject( bool hsig = true );
  720.     
  721.     /**
  722.      * Sets up the completion object to be used.
  723.      *
  724.      * This method assigns the completion object and sets it up to automatically
  725.      * handle the completion and rotation signals internally.  You should use 
  726.      * this function if you want to share one completion object among your
  727.      * widgets or need to use a customized completion object.
  728.      *
  729.      * The object assigned through this method is not deleted when this object's
  730.      * destructor is invoked unless you explicitly call @ref setAutoDeleteCompletionObject
  731.      * after calling this method. Be sure to set the bool argument to false, if
  732.      * you want to handle the completion signals yourself.
  733.      *
  734.      * @param compObj a KCompletion() or a derived child object.
  735.      * @param hsig if true, handles completion signals internally.
  736.      */
  737.     virtual void setCompletionObject( KCompletion* compObj, bool hsig = true );
  738.  
  739.     /**
  740.      * Enables this object to handle completion and rotation
  741.      * events internally.
  742.      *
  743.      * This function simply assigns a boolean value that
  744.      * indicates whether it should handle rotation and
  745.      * completion events or not.  Note that this does not
  746.      * stop the object from emitting signals when these
  747.      * events occur.
  748.      *
  749.      * @param handle if true, handle completion & rotation internally.
  750.      */
  751.     virtual void setHandleSignals( bool handle );
  752.  
  753.     /**
  754.      * Returns true if the completion object is deleted
  755.      * upon this widget's destruction.
  756.      *
  757.      * See setCompletionObject() and enableCompletion()
  758.      * for details.
  759.      *
  760.      * @return true if the completion object will be deleted
  761.      *              automatically
  762.      */
  763.     bool isCompletionObjectAutoDeleted() const {
  764.         return m_delegate ? m_delegate->isCompletionObjectAutoDeleted() : m_bAutoDelCompObj;
  765.     }
  766.  
  767.     /**
  768.      * Sets the completion object when this widget's destructor
  769.      * is called.
  770.      *
  771.      * If the argument is set to true, the completion object
  772.      * is deleted when this widget's destructor is called.
  773.      *
  774.      * @param autoDelete if true, delete completion object on destruction.
  775.      */
  776.     void setAutoDeleteCompletionObject( bool autoDelete ) {
  777.         if ( m_delegate )
  778.             m_delegate->setAutoDeleteCompletionObject( autoDelete );
  779.         else
  780.             m_bAutoDelCompObj = autoDelete;
  781.     }
  782.  
  783.     /**
  784.      * Sets the widget's ability to emit text completion and
  785.      * rotation signals.
  786.      *
  787.      * Invoking this function with @p enable set to @p false will
  788.      * cause the completion & rotation signals not to be emitted.
  789.      * However, unlike setting the completion object to @p NULL
  790.      * using setCompletionObject, disabling the emition of
  791.      * the signals through this method does not affect the current
  792.      * completion object.
  793.      *
  794.      * There is no need to invoke this function by default.  When a
  795.      * completion object is created through completionObject or
  796.      * setCompletionObject, these signals are set to emit
  797.      * automatically.  Also note that disabling this signals will not
  798.      * necessarily interfere with the objects ability to handle these
  799.      * events internally.  See setHandleSignals.
  800.      *
  801.      * @param enable if false, disables the emition of completion & rotation signals.
  802.      */
  803.     void setEnableSignals( bool enable ) {
  804.         if ( m_delegate )
  805.             m_delegate->setEnableSignals( enable );
  806.         else
  807.             m_bEmitSignals = enable;
  808.     }
  809.  
  810.     /**
  811.      * Returns true if the object handles the signals.
  812.      *
  813.      * @return true if this signals are handled internally.
  814.      */
  815.     bool handleSignals() const { return m_delegate ? m_delegate->handleSignals() : m_bHandleSignals; }
  816.  
  817.     /**
  818.      * Returns true if the object emits the signals.
  819.      *
  820.      * @return true if signals are emitted
  821.      */
  822.     bool emitSignals() const { return m_delegate ? m_delegate->emitSignals() : m_bEmitSignals; }
  823.  
  824.     /**
  825.      * Sets the type of completion to be used.
  826.      *
  827.      * The completion modes supported are those defined in
  828.      * KGlobalSettings().  See below.
  829.      *
  830.      * @param mode Completion type:
  831.      *   @li CompletionNone:  Disables completion feature.
  832.      *   @li CompletionAuto:  Attempts to find a match &
  833.      *                        fills-in the remaining text.
  834.      *   @li CompletionMan:   Acts the same as the above
  835.      *                        except the action has to be
  836.      *                        manually triggered through
  837.      *                        pre-defined completion key.
  838.      *   @li CompletionShell: Mimics the completion feature
  839.      *                        found in typical *nix shell
  840.      *                        environments.
  841.      *   @li CompletionPopup: Shows all available completions at once,
  842.      *                        in a listbox popping up.
  843.      */
  844.     virtual void setCompletionMode( KGlobalSettings::Completion mode );
  845.  
  846.     /**
  847.      * Returns the current completion mode.
  848.      *
  849.      * The return values are of type KGlobalSettings::Completion.
  850.      * See setCompletionMode() for details.
  851.      *
  852.      * @return the completion mode.
  853.      */
  854.     KGlobalSettings::Completion completionMode() const {
  855.         return m_delegate ? m_delegate->completionMode() : m_iCompletionMode;
  856.     }
  857.  
  858.     /**
  859.      * Sets the key-binding to be used for manual text
  860.      * completion, text rotation in a history list as
  861.      * well as a completion list.
  862.      *
  863.      *
  864.      * When the keys set by this function are pressed, a
  865.      * signal defined by the inheriting widget will be activated.
  866.      * If the default value or 0 is specified by the second
  867.      * parameter, then the key-binding as defined in the global
  868.      * setting should be used.  This method returns false value
  869.      * for @p key is negative or the supplied key-binding conflicts
  870.      * with the ones set for one of the other features.
  871.      *
  872.      * NOTE: To use a modifier key (Shift, Ctrl, Alt) as part of
  873.      * the key-binding simply simply @p sum up the values of the
  874.      * modifier and the actual key.  For example, to use CTRL+E as
  875.      * a key binding for one of the items, you would simply supply
  876.      * @p "Qt::CtrlButton + Qt::Key_E" as the second argument to this
  877.      * function.
  878.      *
  879.      * @param item the feature whose key-binding needs to be set:
  880.      *   @li TextCompletion the manual completion key-binding.
  881.      *   @li PrevCompletionMatch    the previous match key for multiple completion.
  882.      *   @li NextCompletionMatch    the next match key for for multiple completion.
  883.      *   @li SubstringCompletion  the key for substring completion
  884.      * @param key key-binding used to rotate down in a list.
  885.      * @return true if key-binding can successfully be set.
  886.      * @see getKeyBinding
  887.      */
  888.     bool setKeyBinding( KeyBindingType item , const KShortcut& key );
  889.  
  890.     /**
  891.      * Returns the key-binding used for the specified item.
  892.      *
  893.      * This methods returns the key-binding used to activate
  894.      * the feature feature given by @p item.  If the binding
  895.      * contains modifier key(s), the SUM of the modifier key
  896.      * and the actual key code are returned.
  897.      *
  898.      * @param item the item to check
  899.      * @return the key-binding used for the feature given by @p item.
  900.      * @see setKeyBinding
  901.      */
  902.     const KShortcut& getKeyBinding( KeyBindingType item ) const {
  903.         return m_delegate ? m_delegate->getKeyBinding( item ) : m_keyMap[ item ];
  904.     }
  905.  
  906.     /**
  907.      * Sets this object to use global values for key-bindings.
  908.      *
  909.      * This method changes the values of the key bindings for
  910.      * rotation and completion features to the default values
  911.      * provided in KGlobalSettings.
  912.      *
  913.      * NOTE: By default inheriting widgets should uses the
  914.      * global key-bindings so that there will be no need to
  915.      * call this method.
  916.      */
  917.     void useGlobalKeyBindings();
  918.  
  919.     /**
  920.      * A pure virtual function that must be implemented by
  921.      * all inheriting classes.
  922.      *
  923.      * This function is intended to allow external completion
  924.      * implementations to set completed text appropriately.  It
  925.      * is mostly relevant when the completion mode is set to
  926.      * CompletionAuto and CompletionManual modes. See
  927.      * KCompletionBase::setCompletedText.
  928.      * Does nothing in CompletionPopup mode, as all available
  929.      * matches will be shown in the popup.
  930.      *
  931.      * @param text the completed text to be set in the widget.
  932.      */
  933.     virtual void setCompletedText( const QString& text ) = 0;
  934.  
  935.     /**
  936.      * A pure virtual function that must be implemented by
  937.      * all inheriting classes.
  938.      * @param items the list of completed items
  939.      */
  940.     virtual void setCompletedItems( const QStringList& items ) = 0;
  941.  
  942.     /**
  943.      * Returns a pointer to the completion object.
  944.      *
  945.      * This method is only different from completionObject()
  946.      * in that it does not create a new KCompletion object even if
  947.      * the internal pointer is @p NULL. Use this method to get the
  948.      * pointer to a completion object when inheriting so that you
  949.      * won't inadvertently create it!!
  950.      *
  951.      * @return the completion object or NULL if one does not exist.
  952.      */
  953.     KCompletion* compObj() const { return m_delegate ? m_delegate->compObj() : (KCompletion*) m_pCompObj; }
  954.  
  955. protected:
  956.     /**
  957.      * Returns a key-binding map.
  958.      *
  959.      * This method is the same as getKeyBinding() except it
  960.      * returns the whole keymap containing the key-bindings.
  961.      *
  962.      * @return the key-binding used for the feature given by @p item.
  963.      */
  964.     KeyBindingMap getKeyBindings() const { return m_delegate ? m_delegate->getKeyBindings() : m_keyMap; }
  965.  
  966.     /**
  967.      * Sets or removes the delegation object. If a delegation object is
  968.      * set, all function calls will be forwarded to the delegation object.
  969.      * @param delegate the delegation object, or 0 to remove it
  970.      */
  971.     void setDelegate( KCompletionBase *delegate );
  972.  
  973.     /**
  974.      * Returns the delegation object.
  975.      * @return the delegation object, or 0 if there is none
  976.      * @see setDelegate()
  977.      */
  978.     KCompletionBase *delegate() const { return m_delegate; }
  979.  
  980. private:
  981.     // This method simply sets the autodelete boolean for
  982.     // the completion object, the emit signals and handle
  983.     // signals internally flags to the provided values.
  984.     void setup( bool, bool, bool );
  985.  
  986.     // Flag that determined whether the completion object
  987.     // should be deleted when this object is destroyed.
  988.     bool m_bAutoDelCompObj;
  989.     // Determines whether this widget handles completion signals
  990.     // internally or not
  991.     bool m_bHandleSignals;
  992.     // Determines whether this widget fires rotation signals
  993.     bool m_bEmitSignals;
  994.     // Stores the completion mode locally.
  995.     KGlobalSettings::Completion m_iCompletionMode;
  996.     // Pointer to Completion object.
  997.     QGuardedPtr<KCompletion> m_pCompObj;
  998.     // Keybindings
  999.     KeyBindingMap m_keyMap;
  1000.     // we may act as a proxy to another KCompletionBase object
  1001.     KCompletionBase *m_delegate;
  1002.  
  1003.     // BCI
  1004. protected:
  1005.     virtual void virtual_hook( int id, void* data );
  1006. private:
  1007.     KCompletionBasePrivate *d;
  1008. };
  1009.  
  1010. #endif // KCOMPLETION_H
  1011.